package com.wang.yygh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wang.yygh.cmn.client.DictFeignClient;
import com.wang.yygh.model.hosp.Hospital;
import com.wang.yygh.repository.HospitalRepository;
import com.wang.yygh.service.HospitalService;
import com.wang.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    //上传医院接口
    @Override
    public void save(Map<String, Object> paramMap) {
        //将 参数map集合转换为对象 Hosptial
        String string = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(string, Hospital.class);
        //判断是否存在数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist =  hospitalRepository.getHospitalByHoscode(hoscode);

        //若存在,则修改并保存数据
        if(hospitalExist != null){
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else {  //若不存在，则添加数据
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    //调用service方法实现根据医院编号查询
    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //创建分页对象
        Pageable pageable = PageRequest.of(page-1,limit);
        //创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        //将HospitalQueryVo 对象 转化为 Hospital对象
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);

        //创建对象
        Example<Hospital> example = Example.of(hospital,matcher);

        //调用方法进行查询
        Page<Hospital> pages = hospitalRepository.findAll(example,pageable);

        //查询获取list集合，遍历进行医院等级封装
        pages.getContent().stream().forEach(item -> {
            this.setHospitalHosType(item);
        });

        return pages;
    }

    //获取查询list集合，遍历，进行医院等级封装
    private Hospital setHospitalHosType(Hospital hospital) {
        //根据dictCode 和value 获取医院等级
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        //传入医院地区码，查询省市区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        hospital.getParam().put("hostypeString",hostypeString);
        return  hospital;
    }

    //更新医院上线状态
    @Override
    public void updateStatus(String id, Integer status) {
        //先查询，返回该ID的对象
        Hospital hospital = hospitalRepository.findById(id).get();
        //更改状态、更新时间
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    //医院详情信息
    @Override
    public  Map<String, Object> getHospById(String id) {
        Map<String, Object> result = new HashMap<>();
        //根据ID得到该对象
        Hospital hospital = hospitalRepository.findById(id).get();
        //得到该医院的等级、地区等信息
        Hospital hospital1 = this.setHospitalHosType(hospital);
        result.put("hospital",hospital1);
        //单独处理更直观,预约信息
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    //获取医院名称
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospital != null){
            return  hospital.getHosname();
        }
        return null;
    }
}
